package com.androturka;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import com.androturka.IDataAdapter.EarthQuake;

public class EarthQuakeNotification {

	private static final String ACTION_SHOW_NOTIFICATION_ONMAP 	= "com.listview.showNotificationMap";
	private static final String LATITUDE 							= "latitude";
	private static final String LONGITUDE 						= "longitude";
	private static final String LOCATION 							= "com.listview.location";
	private static final String DATE 								= "com.listview.date";
	private static final String MAGNITUDE 						= "com.listview.magnitude";
	private static final int 	MAXMESAFE 							= 1000;

	final int 				REQUEST_CODE 			= 1;
	int 					notificationID 			= 1;
	int 					notificationFlag		= 0;
	int 					defaultNotificationFlag	= 0;
	ConfigurationManager 	confManager;
	NotificationManager 	manager;
	Notification 			notification;
	PendingIntent 			contentIntent;
	Context 				callerActivity;
	ArrayList<Filter> 		filters 			= new ArrayList<Filter>();
    Context applicationContext;
    Boolean bBarNotification;


	public EarthQuakeNotification(Context applicationContext) {
		
		this.applicationContext = applicationContext;
		confManager = new ConfigurationManager(applicationContext);

		manager = (NotificationManager) applicationContext
				.getSystemService(Context.NOTIFICATION_SERVICE);
		notification = new Notification(R.drawable.icon, "",
				System.currentTimeMillis());

		callerActivity = applicationContext;
		
		filters = confManager.getFilters();
		
		if ( confManager.getRingNotification() )
			defaultNotificationFlag |= Notification.DEFAULT_SOUND;

		if ( confManager.getVibrationNofitication() )
			defaultNotificationFlag |= Notification.DEFAULT_VIBRATE;
		
		notificationFlag |= Notification.FLAG_AUTO_CANCEL;
		
		notificationFlag |= Notification.FLAG_SHOW_LIGHTS;
		
		bBarNotification = confManager.getBarNotification();
		
		confManager.cleanup();
	}

	public void checkFilters(EarthQuake earthquake){
		Filter filter;
		boolean notify = false;
		
        for (int j=0; j < filters.size(); j++) {
        	
        	notify = false;
        	filter = filters.get(j);
        	
        	//Log.d("EQN","earthquake" + "  magnitude : " + earthquake.magnitude + " distance : " + earthquake.distance + " filter magnitude :" + filter.magnitude + " distance : " + filter.distance);
        	if ( ( filter.magnitude != 0 ) && 
        		 ( filter.distance != 0 ) &&
        		 ( earthquake.magnitude >= filter.magnitude ))
        	{
        		if ( earthquake.distance > -1 ){        			
        			if ( earthquake.distance <= filter.distance ) 
        				notify = true;
        		}	
        		else if ( filter.distance == MAXMESAFE)
        			notify = true;
        	}
        	/*
        	else if ( filter.distance != 0 )
        	{
        		if ( ( earthquake.distance > -1 ) && ( earthquake.distance <= filter.distance ) )
        			notify = true;        		
        	}	
        	else if ( filter.magnitude != 0 )
        	{
        		if ( earthquake.magnitude >= filter.magnitude )
        			notify = true;        		
        	}
        	*/
        	
        	if ( notify )
        	{
        		//Log.d("EQN","Filter : " + filter.toString() + " Earthquake : "+ earthquake.location + " - magnitude : " + earthquake.magnitude + "  distance : "+ earthquake.distance);
        		displayNotification(earthquake);
        		return; //no need to check all filters 
        	}	
        }	
	}
	
	public void displayNotification(EarthQuake earthquake) {
	
		displayNotification(earthquake.date, earthquake.latitude, earthquake.longitude, earthquake.depth, earthquake.magnitude, earthquake.location);
	}

	public void displayNotification(Date convertedDate, Double latitude,
			Double longitude, Double depth, Double magnitude, String location) {
		
		if ( bBarNotification )
		{	
			//contentIntent = PendingIntent.getActivity(applicationContext,
			SimpleDateFormat df = new SimpleDateFormat("dd MMMM HH:mm");
			String formattedDate = df.format(convertedDate);

			Intent i = new Intent(applicationContext, Harita.class);
			i.putExtra(LATITUDE, latitude);
			i.putExtra(LONGITUDE, longitude);
			i.putExtra(LOCATION, location);
			i.putExtra(DATE, formattedDate);
			i.putExtra(MAGNITUDE, magnitude);
			i.putExtra("notificationID", notificationID);
			i.setAction(ACTION_SHOW_NOTIFICATION_ONMAP);
		    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

			SharedPreferences prefs = PreferenceManager
					.getDefaultSharedPreferences(callerActivity);
			notificationID = prefs.getInt("NOTIFICATIONID", 1);

		    
			contentIntent = PendingIntent.getActivity(applicationContext,
					notificationID+1, i, PendingIntent.FLAG_CANCEL_CURRENT);

		    notification.setLatestEventInfo(callerActivity, magnitude.toString()
					+ " - " + location,
					convertedDate.toLocaleString(), contentIntent);
		
			notification.defaults |= defaultNotificationFlag;
			notification.flags |= notificationFlag;
	
			//notification.number = notificationID;

			notification.ledARGB = 0xff00ff00;
			notification.ledOnMS = 300;
			notification.ledOffMS = 1000;

			manager.notify(notificationID, notification);
			
			notificationID++;
			
			Editor edit = prefs.edit();
			edit.putInt("NOTIFICATIONID", notificationID);
			edit.commit();

		}
	}	
}
